<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE extension SYSTEM "ext-1.0.dtd">

<!--
/**
 * Adds buttons for easy BBCode formatting.
 *
 * Based on Easy BBCode extension by Rickard Andersson.
 *
 * @copyright (C) 2008-2012 PunBB
 * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
 * @package pun_bbcode
 */
-->

<extension engine="1.0">
	<id>pun_bbcode</id>
	<title>BBCode buttons</title>
	<version>1.4.18</version>
	<description>Pretty buttons for easy BBCode formatting.</description>
	<author>PunBB Development Team</author>

	<minversion>1.4RC1</minversion>
	<maxtestedon>1.4.2</maxtestedon>

	<install><![CDATA[
		if (defined('EXT_CUR_VERSION') && EXT_CUR_VERSION == '1.1')
		{
			// Rename old DB fields
			$forum_db->query('ALTER TABLE '.$forum_db->prefix.'users
				CHANGE use_bbcode_bar pun_bbcode_enabled TINYINT(1) DEFAULT 1,
				CHANGE bbcode_style pun_bbcode_use_buttons TINYINT(1) DEFAULT 1') or error(__FILE__, __LINE__);
		}
		else
		{
			$forum_db->add_field('users', 'pun_bbcode_enabled', 'TINYINT(1)', false, 1);
			$forum_db->add_field('users', 'pun_bbcode_use_buttons', 'TINYINT(1)', false, 1);
		}
	]]></install>


	<uninstall><![CDATA[
		$forum_db->drop_field('users', 'pun_bbcode_enabled');
		$forum_db->drop_field('users', 'pun_bbcode_use_buttons');
	]]></uninstall>


	<hooks>
		<hook id="hd_head"><![CDATA[
			if ($forum_user['pun_bbcode_enabled'] && ((FORUM_PAGE == 'viewtopic' && $forum_config['o_quickpost']) || in_array(FORUM_PAGE, array('post', 'postedit', 'pun_pm-write', 'pun_pm-inbox', 'pun_pm-compose'))))
			{
				if (!defined('FORUM_PARSER_LOADED'))
					require FORUM_ROOT.'include/parser.php';

				// Load CSS
				if ($forum_user['style'] != 'Oxygen' && file_exists($ext_info['path'].'/css/'.$forum_user['style'].'/pun_bbcode.min.css'))
					$forum_loader->add_css($ext_info['url'].'/css/'.$forum_user['style'].'/pun_bbcode.min.css', array('type' => 'url', 'weight' => '90', 'media' => 'screen'));
				else
					$forum_loader->add_css($ext_info['url'].'/css/Oxygen/pun_bbcode.min.css', array('type' => 'url', 'weight' => '90', 'media' => 'screen'));

				// CSS for disabled JS hide bar
				$forum_loader->add_css('#pun_bbcode_bar { display: none; }', array('type' => 'inline', 'noscript' => true));

				// Load JS
				$forum_loader->add_js('PUNBB.pun_bbcode=(function(){return{init:function(){return true;},insert_text:function(d,h){var g,f,e=(document.all)?document.all.req_message:((document.getElementById("afocus")!==null)?(document.getElementById("afocus").req_message):(document.getElementsByName("req_message")[0]));if(!e){return false;}if(document.selection&&document.selection.createRange){e.focus();g=document.selection.createRange();g.text=d+g.text+h;e.focus();}else{if(e.selectionStart||e.selectionStart===0){var c=e.selectionStart,b=e.selectionEnd,a=e.scrollTop;e.value=e.value.substring(0,c)+d+e.value.substring(c,b)+h+e.value.substring(b,e.value.length);if(d.charAt(d.length-2)==="="){e.selectionStart=(c+d.length-1);}else{if(c===b){e.selectionStart=b+d.length;}else{e.selectionStart=b+d.length+h.length;}}e.selectionEnd=e.selectionStart;e.scrollTop=a;e.focus();}else{e.value+=d+h;e.focus();}}}};}());PUNBB.common.addDOMReadyEvent(PUNBB.pun_bbcode.init);', array('type' => 'inline'));

				($hook = get_hook('pun_bbcode_styles_loaded')) ? eval($hook) : null;
			}
		]]></hook>


		<hook id="po_pre_post_contents, vt_quickpost_pre_message_box, ed_pre_message_box, pun_pm_fn_send_form_pre_textarea_output"><![CDATA[
			if ($forum_user['pun_bbcode_enabled'])
			{
				define('PUN_BBCODE_BAR_INCLUDE', 1);
				include $ext_info['path'].'/bar.php';
				$pun_bbcode_bar = new Pun_bbcode;
				$pun_bbcode_bar->render();
			}
		]]></hook>


		<hook id="pf_change_details_settings_validation"><![CDATA[
			$form['pun_bbcode_enabled'] = (!isset($_POST['form']['pun_bbcode_enabled']) || $_POST['form']['pun_bbcode_enabled'] != '1') ? '0' : '1';
			$form['pun_bbcode_use_buttons'] = (!isset($_POST['form']['pun_bbcode_use_buttons']) || $_POST['form']['pun_bbcode_use_buttons'] != '1') ? '0' : '1';
		]]></hook>


		<hook id="pf_change_details_settings_email_fieldset_end"><![CDATA[
			if (file_exists($ext_info['path'].'/lang/'.$forum_user['language'].'/'.$ext_info['id'].'.php'))
				include $ext_info['path'].'/lang/'.$forum_user['language'].'/'.$ext_info['id'].'.php';
			else
				include $ext_info['path'].'/lang/English/pun_bbcode.php';

			$forum_page['item_count'] = 0;
?>
				<fieldset class="frm-group group<?php echo ++$forum_page['group_count'] ?>">
					<div class="sf-set set<?php echo ++$forum_page['item_count'] ?>">
						<div class="sf-box checkbox">
							<span class="fld-input"><input type="checkbox" id="fld<?php echo ++$forum_page['fld_count'] ?>" name="form[pun_bbcode_enabled]" value="1"<?php if ($user['pun_bbcode_enabled'] == '1') echo ' checked="checked"' ?> /></span>
							<label for="fld<?php echo $forum_page['fld_count'] ?>"><span><?php echo $lang_pun_bbcode['Pun BBCode Bar'] ?></span> <?php echo $lang_pun_bbcode['Notice BBCode Bar'] ?></label>
						</div>
					</div>
					<div class="sf-set set<?php echo ++$forum_page['item_count'] ?>">
						<div class="sf-box checkbox">
							<span class="fld-input"><input type="checkbox" id="fld<?php echo ++$forum_page['fld_count'] ?>" name="form[pun_bbcode_use_buttons]" value="1"<?php if ($user['pun_bbcode_use_buttons'] == '1') echo ' checked="checked"' ?> /></span>
							<label for="fld<?php echo $forum_page['fld_count'] ?>"><?php echo $lang_pun_bbcode['BBCode Graphical buttons'] ?></label>
						</div>
					</div>
				</fieldset>
<?php
		]]></hook>
	</hooks>
</extension>
